package cn.com.yusys.yusp.mobile.service;

import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;
import cn.com.yusys.yusp.mobile.repository.mapper.CustomerMapper;
import cn.com.yusys.yusp.mobile.repository.mapper.CustomerVisitMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class CustomerVisitService {

    @Resource
    CustomerVisitMapper customerVisitMapper;

    public List<Map<String, Object>> getCustomerVisitList(QueryModel queryModel){
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        String roleId= (String) queryModel.getCondition().get("roleId");
        String orgId = (String)queryModel.getCondition().get("orgId");
        List<Map<String, Object>> list = null;
        //分行行长查询显示其所有属于其所属的机构下客户
        if (orgId.equals("070667XN0") || orgId.equals("070667XN1") ||orgId.equals("070667860") ||orgId.equals("070667XN3")){
            queryModel.getCondition().put("roleLevel", "1");
            list = customerVisitMapper.getCustomerVisitList(queryModel);
            return list;
        }
        //查询支行下所属的所有客户经理的用户 Sub-branch Manager
        if (roleId.equals("R0001") || roleId.equals("R0002") || roleId.equals("R0009")){
            queryModel.getCondition().put("roleLevel", "3");
            list = customerVisitMapper.getCustomerVisitList(queryModel);
        }
        //查询所有用户
        if (roleId.equals("R0008") || roleId.equals("R0007") || roleId.equals("R001")){
            queryModel.getCondition().put("roleLevel", "0");
            list = customerVisitMapper.getCustomerVisitList(queryModel);
        }
        //支行行长查询，显示其机构下所有客户
        if (roleId.equals("R0006") || roleId.equals("R0005") || roleId.equals("R0003") || roleId.equals("R0004")){
            queryModel.getCondition().put("roleLevel", "2");
            list = customerVisitMapper.getCustomerVisitList(queryModel);
        }

        PageHelper.clearPage();
        return list;
    }

    public Map<String, Object> getCustomerDetail(QueryModel queryModel) {
        Map<String, Object> customerDetail = customerVisitMapper.getCustomerDetail(queryModel);
        return  customerDetail ;
    }

    public Map<String, Object> getCustomerInterview(QueryModel queryModel) {
        Map<String, Object> customerInterview = customerVisitMapper.getCustomerInterview(queryModel);
        return  customerInterview ;
    }


    @Transactional
    public Integer updateCustomerInterview(Map map) {
        if(map.size() ==0){
            return 0;
        }
        Integer updateRecords = customerVisitMapper.updateCustomerInterview(map);
        if(updateRecords>0){
            customerVisitMapper.updateVisitStatus(map);
        }
        return  updateRecords ;

    }
}
